<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="common/common_head :: commonHeader('样品申请')"/>
    <link th:href="@{/static/css/plugins/cropper/cropper.min.css}" rel="stylesheet"/>
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <form class="form-horizontal" id="sampleAddForm">
        <div class="row">
            <div class="form-group">
                <div class="col-sm-8" style="margin-left: 16px">
                    <input class="btn btn-primary" type="button" onclick="save()" value="保存">
                    <button class="btn btn-primary" type="button" onclick="Dialog.closeFrame(window.name)">取 消</button>
                </div>
            </div>

            <!-- 订单抬头信息 -->
            <div id="headerInfo">
                <div class="ibox float-e-margins" style="padding-top: 20px">
                    <div class="form-group">
                        <label class="col-xs-1 control-label">样品类型：</label>
                        <div class="col-xs-3">
                            <select class="form-control" id="itemType" name="itemType" sysDict="YPLX"
                                    emptyFirst="false"></select>
                        </div>
                        <label class="col-sm-1 control-label">采购组：</label>
                        <div class="col-sm-3">
                            <select class="form-control" id="purchGroup" name="purchGroup" sysDict="CGZ"
                                    emptyFirst="false"></select>
                        </div>
                        <label class="col-xs-1 control-label">采购组织</label>
                        <div class="col-xs-3">
                            <select class="form-control" id="purchOrg" name="purchOrg" sysDict="CGZZ"
                                    emptyFirst="false"></select>
                        </div>
                    </div>
                </div>
                <div class="ibox float-e-margins">
                    <div class="table-responsive">
                        <input style="margin-left: 20px" class="btn btn-primary" type="button" onclick="addtr()"
                               value="新增">
                        <table class="table table-bordered text-nowrap" id="itemsTable">
                            <thead>
                            <tr>
                                <th>物料号</th>
                                <th>物料描述<span class="required-sign">*</span></th>
                                <th>数量<span class="required-sign">*</span></th>
                                <th>计量单位</th>
                                <th>物料组</th>
                                <th>工厂</th>
                                <th>库存地点</th>
                                <th>交期<span class="required-sign">*</span></th>
                                <th>内部订单号</th>
                                <th>资产编号</th>
                                <th>试用次数</th>
                                <th>备注</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<!-- Chosen -->
<script th:src="@{/static/js/plugins/chosen/chosen.jquery.js}"></script>
<script type="text/javascript">
    $(function () {
        parseInputDictValToText();//转化数据字典值
        //初始化所有字典表数据
        initDictDataSel();
    });

    var index = 0;

    function addtr() {
        var trCode = "<tr>";
        trCode += "<td width='110px'><input style='width:95%' class='materialCode' name='materialCode' /></td>";
        trCode += "<td width='150px'><input style='width:95%' class='materialDesc'  name='materialDesc' required/></td>";
        trCode += "<td width='110px'><input style='width:95%' type='number' name='applyQty' required/></td>";
        trCode += "<td width='110px'><select class='form-control' class='measureUnit' name='measureUnit' dictType='JLDW' ></select></td>";
        trCode += "<td width='110px'><select class='form-control' class='materialGroup' name='materialGroup' dictType='WLZ' emptyFirst='true'></select></td>";
        trCode += "<td width='150px'><select class='form-control' class='factory' name='factory' dictType='GC' ></select></td>";
        trCode += "<td width='150px'><select class='form-control' class='storageLocation' name='storageLocation' dictType='KCDD'></select></td>";
        trCode += "<td width='110px'><input style='width:95%' id=" + "deliveryDate" + index + " class='deliveryDate' name='deliveryDate' type='text' autocomplete='off' required/></td>";
        trCode += " <td width='110px'><input style='width:95%' type='text' name='internalOrderCode'/></td>";
        trCode += " <td width='110px'><input style='width:95%' type='text' name='mainAssetsCode'/></td>";
        trCode += " <td width='110px'><select style='width:95%' class='form-control' class='reason'  name='reason'>" +
            "<option value='1'>第一次</option><option value='2'>第二次</option><option value='3'>第三次</option></select></td>";
        trCode += " <td width='110px'><input style='width:95%' type='text' name='remark'/></td>";
        trCode += "<td width='110px'><button class='btn btn-danger' type='button'>删除</button></td>";
        trCode += "</tr>";
        $("#itemsTable tbody").append(trCode);
        laydate.render({elem: '#deliveryDate' + index});
        //初始化所有字典表数据
        fillDictDataSel();
        index++;
    }

    //绑定删除按钮事件
    $("#itemsTable").on("click", ".btn-danger", function () {
        $(this).parent().parent().remove();
    });

    //绑定物料光标事件
    $("#itemsTable").on("blur", ".materialCode", function () {
        var materialCode = $(this).val();
        var materialDesc = null;
        if (materialCode != null && materialCode != "") {
            $.ajax({
                url: ctxPath + "/material/getMaterialByCode",
                data: {"materialCode": materialCode},
                dataType: 'JSON',
                async: false,
                success: function (data) {
                    if (data.result) {
                        materialDesc = data.result.materialDesc;
                    } else {
                        Dialog.errorMsg("不存在该物料，请确认!");
                    }
                }
            });
            if (materialDesc != null && materialDesc != "") {
                $(this).parent().parent().find('.materialDesc').val(materialDesc);
            }
        }
    });

    /**
     ** 保存
     */
    function save() {
        var data = {};
        var items = [];
        var divs = $("#headerInfo").find("input, select");
        var trs = $("#itemsTable tbody").find("tr");
        //必填项是否填写标志
        let flag = false;
        trs.each(function (index, tr) {
            var itemEntity = {};
            var its = $(tr).find("input, select");
            its.each(function (idx, it) {
                //循环遍历，把item的内容塞进一个item对象，然后加到数组里
                itemEntity[$(it).attr("name")] = $(it).val();

                if ($(it).attr("name") == "materialDesc" && !$(it).val()) {
                    //没有填写物料描述
                    flag = true;
                } else if ($(it).attr("name") == "poCount" && !$(it).val()) {
                    //没有填写数量
                    flag = true;
                } else if ($(it).attr("name") == "deliveryDate" && !$(it).val()) {
                    //没有填写交期
                    flag = true;
                }
            });
            divs.each(function (idx, it) {
                itemEntity[$(it).attr("name")] = $(it).val();
            });
            items.push(itemEntity);
        });

        if (flag) {
            Dialog.errorMsg("请填写必填项！");
            return false;
        }

        data["items"] = items;
        if (trs.length <= 0) {
            Dialog.errorMsg("请先添加行项目数据!");
            return;
        }
        layer.load(1, {shade: [0.3, '#393D49']});
        $.ajax({
            url: ctxPath + "/sampleRequest/add",
            data: {"datalist": JSON.stringify(data)},
            dataType: "json",
            success: function (data) {
                layer.closeAll();
                if (data.code == 0) {
                    alert("样品申请单号为:" + data.result);
                    parent.itemBS.refresh();
                    Dialog.closeFrame(window.name);
                } else {
                    Dialog.errorMsg("生成失败!");
                }
            }
        });
    }

</script>

</body>
</html>
